﻿using DevExpress.XtraEditors;
using Lamex.Base;
using Lamex.Common;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Lamex.mIE.Calculate
{
    /* 1、BOM物料在维护工艺路线时根据物料号得到B_IE_PACKAGE_BOM_DATA_SJR/B_IE_PACKAGE_BOM_DATA_STD表相关数据
     * 2、返回符合条件的Datatable，方便后续取值计算
     * 3、非标（物料号第2-3位为"96"）表为[B_IE_PACKAGE_BOM_DATA_SJR]，标准（非96物料）表为[B_IE_PACKAGE_BOM_DATA_STD]。以下简为[BOM]。
     */
    public class LGC_VAL_BOM
    {
        #region JACK 批量导入专用 一次性
        public static DataTable LGC_VAL_BOM_VALUE(string Version, string DWG_No, string Part_No, string strRouting, string Feature_Name, out double Feature_Value, out string Error_Code)

        {
            Feature_Value = 0;
            Error_Code = "";
            try
            {

                DataTable dt = new DataTable();
                DataTable dtResult = new DataTable();
                DataTable dtResults = new DataTable();
                string IsRounting = strRouting;
                if (Part_No.Length > 2)
                {
                    //获取参考物料号码
                    DataTable dtMat = BaseBuildSql.GetDataTableBySql(@" select REF_MAT FROM [LamexMESManager].[dbo].[IE_Part_TaskList] where PART_NO='" + Part_No + "'");
                    string Ref_MatNo = dtMat.Rows[0]["REF_MAT"].ToString().Trim();
                    if (Part_No.Substring(1, 2).Contains("96"))
                    {
                        dt = BaseBuildSql.GetDataTableBySql("select * from B_IE_PACKAGE_BOM_DATA_SJR where MATERIAL_NO='" + Part_No + "'");
                        if (dt.Rows.Count <= 0)
                        {
                            dt = BaseBuildSql.GetDataTableBySql("select * from B_IE_PACKAGE_BOM_DATA_SJR where MATERIAL_NO='" + Ref_MatNo + "'");
                            if (dt.Rows.Count <= 0)
                            {
                                Error_Code = "当前物料BOM表无数据，请确认！";
                                return dtResults;
                            }
                        }
                    }
                    else
                    {
                        dt = BaseBuildSql.GetDataTableBySql("select * from B_IE_PACKAGE_BOM_DATA_STD where MATERIAL_NO='" + Part_No + "' and Version='" + Version + "' ");
                        if (dt.Rows.Count <= 0)
                        {
                            dt = BaseBuildSql.GetDataTableBySql("select * from B_IE_PACKAGE_BOM_DATA_STD where MATERIAL_NO='" + Part_No + "' and ISNULL(Version,'')='' ");
                            if (dt.Rows.Count <= 0)
                            {
                                if (string.IsNullOrEmpty(Ref_MatNo) || Ref_MatNo == "NIL")
                                {
                                    Error_Code = "当前物料BOM表无数据，请确认！";
                                    return dtResults;
                                }
                                else
                                {
                                    dt = BaseBuildSql.GetDataTableBySql("select * from B_IE_PACKAGE_BOM_DATA_STD where MATERIAL_NO='" + Ref_MatNo + "' and Version='" + Version + "' ");
                                    if (dt.Rows.Count <= 0)
                                        dt = BaseBuildSql.GetDataTableBySql("select * from B_IE_PACKAGE_BOM_DATA_STD where MATERIAL_NO='" + Ref_MatNo + "' and ISNULL(Version,'')='' ");
                                    if (dt.Rows.Count <= 0)
                                    {
                                        Error_Code = "当前物料BOM表无数据，请确认！";
                                        return dtResults;
                                    }
                                }
                            }
                        }
                    }

                }
                if (Feature_Name.Length > 0)
                {
                    string sql = @"select PROCESS_CODE  FROM IE_WorkCenter_Feature where FEATURE_NAME='" + Feature_Name + "'";
                    DataTable dtTemp = BaseBuildSql.GetDataTableBySql(sql);
                    if (dtTemp.Rows.Count > 0)
                    {
                        strRouting = dtTemp.Rows[0]["PROCESS_CODE"].ToString();
                    }
                }
                else
                {


                    string sql = @" SELECT * FROM [dbo].[IE_Routing_Combination] WHERE GROUP_CODE ='" + strRouting + "'";
                    DataTable dtTemp = BaseBuildSql.GetDataTableBySql(sql);
                    if (dtTemp.Rows.Count > 0)
                    {
                        strRouting = dtTemp.Rows[0]["ROUTING_CODE"].ToString();
                    }
                }

                string[] rountingArr = strRouting.Split('_');

                foreach (string s in rountingArr)
                {
                    if (s == "P71")
                    {
                        dtResult = Calculate.LGC_VAL_BOM_P71.LGC_VAL_BOM_P71_VALUE(DWG_No, Part_No, "P71", "LGC_TIM_VTA");

                    }
                    else if (s == "P73")
                    {
                        dtResult = Calculate.LGC_VAL_BOM_P73.LGC_VAL_BOM_P73_VALUE(
                                    dt, DWG_No, Part_No, "P73", "LGC_TIM_VTA", out Error_Code);
                    }
                    else if (s == "P74")
                    {
                        dtResult = Calculate.LGC_VAL_BOM_P74.LGC_VAL_BOM_P74_VALUE(
                                dt, DWG_No, Part_No, "P74", "LGC_TIM_VTA");
                    }
                    else if (s == "P75")
                    {
                        dtResult = Calculate.LGC_VAL_BOM_P75.LGC_VAL_BOM_P75_VALUE(
                                 dt, DWG_No, Part_No, "P75", "LGC_TIM_VTA");
                    }
                    else if (s == "P76")
                    {
                        dtResult = Calculate.LGC_VAL_BOM_P76.LGC_VAL_BOM_P76_VALUE(DWG_No, Part_No, "P76", "LGC_TIM_VTA", out Error_Code);

                    }
                    else if (s == "P79")
                    {
                        dtResult = Calculate.LGC_VAL_BOM_P79.LGC_VAL_BOM_P79_VALUE(
                                 dt, DWG_No, Part_No, "P79", "LGC_TIM_VTA");
                    }
                    if (dtResult != null)
                        dtResults.Merge(dtResult);



                }

                //单独返值
                if (dtResult.Rows.Count > 0 && Feature_Name.Length > 0)
                {
                    Feature_Value = (dtResult.Select("FEATURE_NAME='" + Feature_Name + "'").Length > 0 ?
                       Convert.ToDouble(dtResult.Select("FEATURE_NAME='" + Feature_Name + "'").CopyToDataTable().Rows[0]["LABOR_VALUE"].ToString()) : 0);
                }

                //临时判断
                if (Part_No == "3000000000")
                    Feature_Value = 10;


                return dtResults;
            }
            catch (Exception ex)
            {
                //XtraMessageBox.Show(Part_No+strRouting+ ex.Message);
                return null;
            }

        }
        #endregion

        public static DataTable LGC_VAL_BOM_VALUE_ALL(string Version, string DWG_No, string Part_No, string strRouting, string Feature_Name, string[] strRoutingList, out string Error_Code)
        {          
            Error_Code = "";
            try
            {

                DataTable dt = new DataTable();
                DataTable dtResult = new DataTable();
                DataTable dtResults = new DataTable();

                //获取参考物料号码
                DataTable dtMat = BaseBuildSql.GetDataTableBySql(@" select REF_MAT FROM [LamexMESManager].[dbo].[IE_Part_TaskList] where PART_NO='" + Part_No + "'");
                string Ref_MatNo = dtMat.Rows[0]["REF_MAT"].ToString().Trim();
                if (Part_No.Substring(1, 2).Contains("96"))
                {
                    dt = BaseBuildSql.GetDataTableBySql("select * from B_IE_PACKAGE_BOM_DATA_SJR where MATERIAL_NO='" + Part_No + "'");
                    if (dt.Rows.Count <= 0)
                    {
                        dt = BaseBuildSql.GetDataTableBySql("select * from B_IE_PACKAGE_BOM_DATA_SJR where MATERIAL_NO='" + Ref_MatNo + "'");
                        if (dt.Rows.Count <= 0)
                        {
                            Error_Code = "当前物料BOM表无数据，请确认！";
                            return dtResults;
                        }
                        else
                            Part_No = Ref_MatNo;//2020.11.2 增加逻辑：用参考物料计算
                    }
                }
                else
                {
                    dt = BaseBuildSql.GetDataTableBySql("select * from B_IE_PACKAGE_BOM_DATA_STD where MATERIAL_NO='" + Part_No + "' and Version='" + Version + "' ");
                    if (dt.Rows.Count <= 0)
                    {
                        dt = BaseBuildSql.GetDataTableBySql("select * from B_IE_PACKAGE_BOM_DATA_STD where MATERIAL_NO='" + Part_No + "' and ISNULL(Version,'')='' ");
                        if (dt.Rows.Count <= 0)
                        {
                            if (string.IsNullOrEmpty(Ref_MatNo) || Ref_MatNo == "NIL")
                            {
                                Error_Code = "当前物料BOM表无数据，请确认！";
                                return dtResults;
                            }
                            else
                            {
                                dt = BaseBuildSql.GetDataTableBySql("select * from B_IE_PACKAGE_BOM_DATA_STD where MATERIAL_NO='" + Ref_MatNo + "' and Version='" + Version + "' ");
                                if (dt.Rows.Count <= 0)
                                    dt = BaseBuildSql.GetDataTableBySql("select * from B_IE_PACKAGE_BOM_DATA_STD where MATERIAL_NO='" + Ref_MatNo + "' and ISNULL(Version,'')='' ");
                                if (dt.Rows.Count <= 0)
                                {
                                    Error_Code = "当前物料BOM表无数据，请确认！";
                                    return dtResults;
                                }
                                else
                                    Part_No = Ref_MatNo;//2020.11.2 增加逻辑：用参考物料计算

                            }
                        }
                    }
                }

                switch (strRouting)
                {
                    case "P71":
                        dtResult = Calculate.LGC_VAL_BOM_P71.LGC_VAL_BOM_P71_VALUE(DWG_No, Part_No, "P71", "LGC_TIM_VTA");
                        break;
                    case "P73":
                        dtResult = Calculate.LGC_VAL_BOM_P73.LGC_VAL_BOM_P73_VALUE(
                            dt, DWG_No, Part_No, "P73", "LGC_TIM_VTA", out Error_Code);
                        break;
                    case "P74":
                        dtResult = Calculate.LGC_VAL_BOM_P74.LGC_VAL_BOM_P74_VALUE(
                            dt, DWG_No, Part_No, "P74", "LGC_TIM_VTA");
                        break;
                    case "P75":
                        dtResult = Calculate.LGC_VAL_BOM_P75.LGC_VAL_BOM_P75_VALUE(
                             dt, DWG_No, Part_No, "P75", "LGC_TIM_VTA");
                        break;
                    case "P76":
                        dtResult = Calculate.LGC_VAL_BOM_P76.LGC_VAL_BOM_P76_VALUE(DWG_No, Part_No, "P76", "LGC_TIM_VTA", out Error_Code);
                        break;
                    case "P79":
                        dtResult = Calculate.LGC_VAL_BOM_P79.LGC_VAL_BOM_P79_VALUE(
                             dt, DWG_No, Part_No, "P79", "LGC_TIM_VTA");
                        break;
                    case "D44":
                        dtResult = Calculate.LGC_VAL_BOM_D44.LGC_VAL_BOM_D44_VALUE(DWG_No, Part_No, "D44", "LGC_TIM_VTA", dt);
                        break;
                    case "D47":
                        dtResult = Calculate.LGC_VAL_BOM_D47.LGC_VAL_BOM_D47_VALUE(DWG_No, Part_No, "D47", "LGC_TIM_VTA", dt);
                        break;
                    case "D51":
                        dtResult = Calculate.LGC_VAL_BOM_D51.LGC_VAL_BOM_D51_VALUE(DWG_No, Part_No, "D51", "LGC_TIM_VTA", dt, strRoutingList);
                        break;
                    case "D54":
                        dtResult=Calculate.LGC_VAL_BOM_D54.LGC_VAL_BOM_D54_VALUE(DWG_No, Part_No, "D54", "LGC_TIM_VTA", dt);
                        break;

                }
                return dtResult;
            }
            catch (Exception ex)
            {
                return null;
            }


        }
    }
}
